﻿#ifndef DATAMANAGER_H
#define DATAMANAGER_H

#include <QObject>
#include <QMap>
#include "common/datatypes.h"

class DatabaseManager;

class DataManager : public QObject
{
    Q_OBJECT
public:
    static DataManager *instance();

    void initialize();

    void selectTestTemplate(int id);
    // 当用户在UI选择一个硬件模板时调用
    void selectHardwareTemplate(int id);
    const HardwareTemplatePtr getFullHardwareTemplate(int id);
    const TestTemplatePtr getFullTestTemplate(int id);

    const QList<OverviewItem> &getParameterTableOverviews() const;
    const QList<OverviewItem> &getHardwareTemplateOverviews() const;
    const QList<OverviewItem> &getTestTemplateOverviews() const;

    HardwareTemplatePtr getSelectedHardwareTemplate() const;
    TestTemplatePtr getSelectedTestTemplate() const;

    AttTableList getAttTable(int tableId);
    TransducerTableList getTransducerTable(int tableId);
    LimitLineList getLimitLineTable(int tableId);
    QList<FreqTableConfig> getFreqTable(int tableId);
    QList<CalTableConfig> getCalibrationTable(int tableId);

    TestTemplatePtr addTestTemplate(const QString &name);
    bool removeTestTemplate(int id);
    bool updateTestTemplate(TestTemplatePtr tpl);
    TestTemplatePtr duplicateTestTemplate(int sourceTemplateId);

    HardwareTemplatePtr addHardwareTemplate(const QString &name, HardwareTemplateType type);
    bool removeHardwareTemplate(int id);
    bool updateHardwareTemplate(HardwareTemplatePtr hwTemplate);
    HardwareTemplatePtr duplicateHardwareTemplate(int sourceTemplateId);
    bool hardwareTemplateNameExists(const QString &name) const;

    bool addParameterTable(OverviewItem &item);
    bool removeParameterTable(int id, const ParamTableKind type);
    bool parameterTableNameExists(const QString &name, const ParamTableKind kind) const;
    OverviewItem duplicateParameterTable(int sourceTableId, ParamTableKind kind);

    bool saveAttTable(int tableId, const QString &name, const AttTableList &t);
    bool saveTransducerTable(int tableId, const QString &name, const TransducerTableList &t);
    bool saveLimitLineTable(int tableId, const QString &name, const LimitLineList &t);
    bool saveFreqTable(int tableId, const QString &name, const QList<FreqTableConfig> &rows);
    bool saveCalibrationTable(int tableId, const QString &name, const QList<CalTableConfig> &rows);

    QStringList getDeviceModelList(const DeviceRole role);
    QStringList getDeviceModelList(const DeviceType Type);

    DeviceRole getDeiceRoleForModel(const QString &model) const;
    DeviceType getDeiceTypeForModel(const QString &model) const;
    QString libraryPathForModel(const QString &model) const;

    bool overVivewNameExists(const QList<OverviewItem> &list, const QString &name) const;

    int findIDByOverViewName(const QList<OverviewItem> &list, const ParamTableKind type, const QString &name) const;
    QString findNameByOverViewID(const QList<OverviewItem> &list, const ParamTableKind type, int id) const;

    QString getParamTableEnumString(const ParamTableKind type);

signals:
    void hardwareTemplatesOverviewReady();

    void testTemplatesOverviewReady();
    void calibrationFilesOverviewReady();

    void selectedHardwareTemplateChanged();
    void selectedTestTemplateChanged();

    void sig_hardwareTemplatesChanged();
    void sig_parameterTablesChanged();
    void sig_testTemplatesChanged();

private:
    explicit DataManager(QObject *parent = nullptr);
    DataManager(const DataManager &) = delete;
    DataManager &operator=(const DataManager &) = delete;
    bool insertDevice(DeviceConfigBase &device, int hardwareTemplateId, DeviceRole role);

    const HardwareTemplatePtr loadAndCacheHardwareTemplate(int id);
    const TestTemplatePtr loadAndCacheTestTemplate(int id);
    bool loadAndCacheDeviceModel();

private:
    QList<OverviewItem> m_hardwareTemplateOverviews;
    QList<OverviewItem> m_parameterTableOverviews;
    QList<OverviewItem> m_testTemplateOverviews;

    HardwareTemplatePtr m_selectedHardwareTemplate;
    TestTemplatePtr m_selectedTestTemplate;

    QMap<DeviceRole, QStringList> m_deviceModelCache;
    QMap<QString, DeviceType> m_modelToDeviceTypeCache;
    QMap<QString, QString> m_modelToLibraryPathCache;

    QMap<int, DeviceConfigBasePtr> m_deviceCache;
    QMap<int, HardwareTemplatePtr> m_hardwareTemplateCache;
    QMap<int, TestTemplatePtr> m_testTemplateCache;

    DatabaseManager *m_dbManager;
};

#endif // DATAMANAGER_H
